﻿/**
 * Created by Administrator on 2017/8/25.
 */
define(['jquery'],function ($) {
    var InputContact = {
        name: 'input-contact',
        template: ''
        + '<span>'
        + '    <em  v-for="(item, index) in contactList"  v-bind:class="{showOtherMobile:index>0}">'
        + '         <i v-bind:class="{selectedI:item.IsUsually}" v-on:click="changeUaually(index)" style="cursor:pointer" ></i>'
        + '         <em v-bind:class="{selectedEM:item.IsUsually}"  v-on:click="changeUaually(index)"  style="cursor:pointer">{{contactTypeText}}：</em>'
        + '         <input type="text" class="inputPhone" maxlength="11" style="margin-left: -4px;" '
        + '         v-on:focus="handleFocus" v-on:blur="handleBlur" v-model="item.ContactValue" /></em>'
        + '</span>',
        props: {
            value: {
                type: Array,
                default: []
            },
            contactType: {
                type: Number,
                default: 0
            },
            contactTypeText: {
                type: String,
                default: "手机"
            },
        },
        data: function () {
            return {
                contactList: this.value,
            };
        },
        methods: {
            handleFocus: function (event) {
                var focusValue = event.target.value;
                if ($.trim(focusValue) === "") {
                    var contactEmpty = {
                        Type: this.contactType,
                        TypeText: this.contactTypeText,
                        IsUsually: false,
                        ContactValue: ""
                    };
                    this.contactList.push(contactEmpty);
                }
            },
            handleBlur: function (event) {
                var tempList = this.contactList;
                this.contactList = [];
                this.value = [];
                for (var i = 0; i < tempList.length; i++) {
                    if ($.trim(tempList[i].ContactValue) != "") {
                        tempList[i].ContactValue = $.trim(tempList[i].ContactValue);
                        this.contactList.push(tempList[i]);
                        this.value.push(tempList[i]);
                    }
                }
                this.$emit('input', this.value);
                var contactEmpty = {
                    Type: this.contactType,
                    TypeText: this.contactTypeText,
                    IsUsually: false,
                    ContactValue: ""
                };
                this.contactList.push(contactEmpty);
                var c = 0;
                for (var j = 0; j < this.contactList.length; j++) {
                    if (this.contactList[j].IsUsually) {
                        c++;
                    }
                }
                if (c === 0)
                {
                    this.contactList[0].IsUsually = true;
                }
                //触发on-blur
                this.$emit("on-blur");
            },
            changeUaually: function (index) {
                if ($.trim(this.contactList[index].ContactValue) === "") {
                    this.$Message.error("不能指定空的");
                    return;
                }
                for (var j = 0; j < this.contactList.length; j++) {
                    this.contactList[j].IsUsually = false;
                    if (j===index) {
                        this.contactList[j].IsUsually = true;
                    }
                }
            }
        }
    };
    return InputContact;
});
